define(function (require) {
var zsApp = require('app');
require('selectCache');
zsApp.controller('privilegeCtrl', ['$scope', '$http', '$timeout', 'selectCache', 'promptCache', function($scope, $http, $timeout, selectCache, promptCache) {
	
	// pagination
	$scope.paginationConf = {
        currentPage     : 1,
        totalItems      : 10,
        itemsPerPage    : 10,
        pagesLength     : 15,
        perPageOptions  : [10, 20, 30, 40, 50],
        onChange: function() { 
        	var self = this,
	        	pagingData = {
	        		pageNumber  : self.currentPage,  
					pageSize 	: self.itemsPerPage,  
	        	};	
			$http
			({
				url  	: $scope.zsUrl+'digitalhome-boss/admin/operations/getList',
				method 	: 'POST',
				data 	:  {
                    pageNumber  : self.currentPage,  
                    pageSize    : self.itemsPerPage,  
                }

			}).success(function(res) {

                if(res.result_code != '0000') {
                    $scope.isNoData = false;
                    return false;
                }

                // 表格数据
                $scope.tabVal = res.result;

                angular.forEach($scope.tabVal, function(obj, i) {
                    // 遍历每条数据对应的ID
                    $scope.dataID[i]   = obj.operationId;
                });

                // 总共有多少条数据
                self.totalItems = res.length;

            }).error(function(err) {
            	// console.log(err);
            });
        }
    };
	
	// 确定新增
	$scope.submitNewlyAdded = function() {
		
		var operationData = {
			operationCode   : $scope.privilege_operationCode,
			operationName   : $scope.privilege_operationName,
			enable 			: $scope.privilege_enable,
			createUserId  	: $scope.privilege_createUserId,
			description   	: $scope.privilege_description,
		};

		$http
		({
			url    : $scope.zsUrl+'digitalhome-boss/admin/operations/add',
			method : 'POST',
			data   : operationData, 
 			
		}).success(function(res) {		
			
			if( '0000' != res.result_code ) {
				promptCache.errorPrompt(res.message);
				return false;
			}

			promptCache.successPrompt(res.message);
			reloadTable();

		}).error(function(err) {
			promptCache.errorPrompt('请求失败！');
			return false;
		});

		// 关闭窗口
		$('#newlyAdded').modal('hide');
	};

	// 详情
    $scope.details = function(i) {
    	$http
    	({
    		url 	: $scope.zsUrl+'digitalhome-boss/admin/operations/getInfos/'+$scope.dataID[i],
    		method	: 'GET',

    	}).success(function(res) {

    		if( '0000' != res.result_code ) {
    			return false;
    		}

    		$scope.details_operationCode = res.result.operationCode;
    		$scope.details_operationName = res.result.operationName;

    		var status = res.result.enable;

    		if(status == '1'){
    			status = "启用";
    		}else{
    			status = "禁用";
    		}

    		$scope.details_enable = status;
    		$scope.details_createUserId = res.result.createUserId;
    		$scope.details_description = res.result.description;

    	}).error(function(err) {
    		promptCache.errorPrompt('请求失败！');
			return false;
    	});
    };
	
	 // 修改（接口与"详情"相同）
	 $scope.edit = function(i) {

    	$http
    	({
    		url 	: $scope.zsUrl+'digitalhome-boss/admin/operations/getInfos/'+$scope.dataID[i],
    		method	: 'GET',
    	}).success(function(res) {

    		if( '0000' != res.result_code ) {
    			return false;
    		}

    		$scope.index = i;
    		
    		$scope.edit_operationCode = res.result.operationCode;
    		$scope.edit_operationName = res.result.operationName;
    		$scope.edit_enable = res.result.enable;
    		$scope.edit_description = res.result.description;
    		$scope.editOperationEnableSelect = 
    		[
    			{ 'id' : 0, 'name': '禁用' },
    			{ 'id' : 1, 'name': '启用' },
    		];

    	}).error(function(err) {
    		promptCache.errorPrompt('请求失败！');
			return false;
    	});
    };

	// 确认修改
	$scope.confirmEdit = function($event) {
        
		reloadTable();

		var editData = {
				operationId   		: $scope.dataID[$scope.index],
				operationCode   	: $scope.edit_operationCode,
				operationName   	: $scope.edit_operationName,
				enable 				: $scope.edit_enable,
				createUserId  		: $scope.edit_createUserId,
				description   		: $scope.edit_description,
				lastUpdatedUserId 	: $scope.edit_lastUpdatedUserId,
		};

		$http
		({
			url   	: $scope.zsUrl+'digitalhome-boss/admin/operations/modify',
			method  : 'POST',
			data    : editData, 

		}).success(function(res){
			
			if( '0000' != res.result_code ) {
				promptCache.errorPrompt(res.message);
				return false;
			}

			promptCache.successPrompt(res.message);

		}).error(function(err) {
			promptCache.errorPrompt('修改失败！');
			return false;
		});

	};
	
    // 删除 启用 禁用
    $scope.getDataID = function() {

    	// data length
    	var len = $scope.dataID.length;
    	// param id
    	var whichId = 'operationId';
    	// param
    	var deleteUrl = 'admin/operations/delete';
    	// enable disabled
        var isEnableUrl = 'admin/operations/modifyStatus';

    	// multiple selections 
        $scope.selectAll= function (bool, obj) {
            selectCache.selectAll(bool, $scope.dataID);
        };

        //  Single or multiple selections 
        $scope.select= function (selectObj, bool) {   
         	var selectId = selectObj.operationId;        
            selectCache.select(selectId, bool, len);    
        }; 

        // check
        $scope.check= function () { 
            selectCache.check();
        };

        // delete
        $scope.delete= function () { 
            selectCache.delete(whichId, deleteUrl);
            reloadTable();
        };

        // enable
        $scope.enable= function () { 
            selectCache.isEnable(whichId, isEnableUrl, 1);
            reloadTable();
        };

        // disabled
        $scope.disabled= function () { 
            selectCache.isEnable(whichId, isEnableUrl, 0);
            reloadTable();
        };
    };

    // reload table
    function reloadTable() {
        $timeout(function() {
            $scope.paginationConf.onChange();
            $scope.master = false;
        },1500);
    };
}])

.directive('privilegeNewlyadded', function() {
	return {
		restrict		: 'E',
		templateUrl		: 'user/privilege/newlyAdded.html',
		replace			: true,
	}
})
.directive('privilegeDetails', function() {
	return {
		restrict		: 'E',
		templateUrl		: 'user/privilege/details.html',
		replace			: true,
	}
})
.directive('privilegeEdit', function() {
	return {
		restrict		: 'E',
		templateUrl		: 'user/privilege/edit.html',
		replace			: true,
	}
})
.directive('privilegeDelete', function() {
	return {
		restrict		: 'E',
		templateUrl		: 'user/privilege/delete.html',
		replace			: true,
	}
})
.directive('privilegeTable', function() {
	return {
		restrict		: 'E',
		templateUrl		: 'user/privilege/table.html',
		replace			: true,
	}
});
});